﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace JTLeigh.Commerce.Spencer.Features.ProductFeedback.Business.UnitTests
{

	/// <summary>
	/// Unit tests for the <see cref="BreakdownAveragingCounter"/> class.
	/// </summary>
	[TestClass]
	public class BreakdownAveragingCounterFixture
	{

		#region Methods

		/// <summary>
		/// Tests all methods and properties of the counter.
		/// </summary>
		[TestMethod]
		public void TestBreakdownAveragingCounterOperation()
		{
			BreakdownAveragingCounter counter;
			Dictionary<byte, int> breakdown;

			counter = new BreakdownAveragingCounter();
			Assert.AreEqual(0, counter.Sum);
			Assert.AreEqual(0, counter.Count);
			Assert.IsNull(counter.Average);
			Assert.AreEqual(0, counter.Breakdown.Count);

			counter.AddValue(1);
			Assert.AreEqual(1, counter.Sum);
			Assert.AreEqual(1.0d, counter.Average);
			breakdown = counter.Breakdown;
			Assert.AreEqual(1, breakdown.Count); // 1 value so far
			Assert.IsTrue(breakdown.ContainsKey(1));
			Assert.AreEqual(1, breakdown[1]);

			counter.AddValue(2);
			Assert.AreEqual(3, counter.Sum);
			Assert.AreEqual(1.5d, counter.Average);
			breakdown = counter.Breakdown;
			Assert.AreEqual(2, breakdown.Count); // 2 values now
			Assert.IsTrue(breakdown.ContainsKey(1));
			Assert.AreEqual(1, breakdown[1]); // 1 instance of value '1' so far
			Assert.IsTrue(breakdown.ContainsKey(2));
			Assert.AreEqual(1, breakdown[2]);

			counter.AddValue(1);
			Assert.AreEqual(4, counter.Sum);
			Assert.AreEqual(1.33d, Math.Round(counter.Average.Value, 2));
			Assert.AreEqual(2, breakdown.Count); // still 2 values
			Assert.IsTrue(breakdown.ContainsKey(1));
			Assert.AreEqual(2, breakdown[1]); // 2 instances of value '1' now
			Assert.IsTrue(breakdown.ContainsKey(2));
			Assert.AreEqual(1, breakdown[2]);

			counter.Reset();
			Assert.AreEqual(0, counter.Sum);
			Assert.AreEqual(0, counter.Count);
			Assert.IsNull(counter.Average);
		}

		#endregion // Methods

	}

}
